﻿using MvcOSGi.Shell.Infra;
using MvcOSGi.Shell.Version;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using BusinessLayer;

namespace MvcOSGi.Shell.Models
{
    public class PageViewModel : IDisposable
    {

        public PageViewModel(Dictionary<BusinessLayer.Enums.DataBagKey, IMultipleResults> dataBag)
        {
            DataBag = dataBag;
            PageInfo = dataBag.GetResult<Page>(BusinessLayer.Enums.DataBagKey.BaseData).FirstOrDefault();
            VersionInfo = dataBag.GetResult<Version.Version>(BusinessLayer.Enums.DataBagKey.BaseData).FirstOrDefault();
        }

        private bool disposed = false;
        public Page PageInfo { get; set; }
        public Version.Version VersionInfo { get; set; }
        public Dictionary<BusinessLayer.Enums.DataBagKey, IMultipleResults> DataBag { get; set; }

        private IEnumerable<T> GetResult<T>(BusinessLayer.Enums.DataBagKey key) { return DataBag.GetResult<T>(key); }

        public void Dispose()
        {
            Dispose(true);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposed) return;
            if (disposing)
            {
                DataBag.ToList().ForEach(i => i.TryDispose());
            }
            disposed = true;
        }
    }
}